package drive.Mapper;

import drive.Model.FollowRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FollowRelationMapper {
    // 查询所有关注关系
    @Select("SELECT * FROM followrelation")
    List<FollowRelation> findAll();

    // 根据followerID和followedID查询关注关系
    @Select("SELECT * FROM followrelation WHERE followerID = #{followerID} AND followedID = #{followedID}")
    FollowRelation findByFollowerAndFollowed(int followerID, int followedID);

    // 根据followerID查询关注关系
    @Select("SELECT * FROM followrelation WHERE followerID = #{followerID}")
    List<FollowRelation> findByFollowerID(int followerID);

    // 根据followedID查询关注关系
    @Select("SELECT * FROM followrelation WHERE followedID = #{followedID}")
    List<FollowRelation> findByFollowedID(int followedID);

    // 新增关注关系
    @Insert("INSERT INTO followrelation (followerID, followedID, createTime) VALUES (#{followerID}, #{followedID}, #{createTime})")
    int add(FollowRelation followRelation);

    // 更新关注关系
    @Update("UPDATE followrelation SET createTime = #{createTime} WHERE followerID = #{followerID} AND followedID = #{followedID}")
    int update(FollowRelation followRelation);

    // 删除关注关系
    @Delete("DELETE FROM followrelation WHERE followerID = #{followerID} AND followedID = #{followedID}")
    int delete(int followerID, int followedID);

    @Select("SELECT COUNT(*) FROM followrelation WHERE followedID = #{userId}")
    int getFollowerCount(@Param("userId") int userId);

    @Select("SELECT COUNT(*) FROM followrelation WHERE followerID = #{userId}")
    int getFollowingCount(@Param("userId") int userId);

    /**
     * 根据用户ID获取被关注的用户ID列表
     * @param userId 用户ID
     * @return 被关注的用户ID列表
     */
    @Select("SELECT followedID FROM followrelation WHERE followerID = #{userId}")
    List<Integer> getFollowedUserIds(int userId);


}